package com.property.manage.controller.flowingWater;

import com.guardchina.framework.mybatis.model.CustomPage;
import com.property.manage.enums.WalletType;
import com.property.manage.model.flowingWater.*;
import com.property.manage.model.print.PayBillVo;
import com.property.manage.model.resource.ProBuildingVo;
import com.property.manage.model.system.SysSet;
import com.property.manage.service.flowingWater.FlowingWaterService;
import com.property.manage.service.system.SysSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述
 *
 * @author lwb
 * @since 2020/7/15
 */
@Api(tags = "财务流水")
@RestController
@RequestMapping("/flowingWater")
@AllArgsConstructor
@Validated
@Slf4j
public class FlowingWaterController {


    @Autowired
    private FlowingWaterService flowingWaterService;
    @Autowired
    private SysSetService sysSetService;

    @ApiOperation("收费类型")
    @GetMapping("/type")
    public List<SysSet> type() {
        List<SysSet> sysSets = sysSetService.getSysSets(SysSet.COLLECTION_TYPE);

        return sysSets.stream().filter(sysSet -> !"1".equals(sysSet.getValue()) && !"2".equals(sysSet.getValue()) && !"3".equals(sysSet.getValue())).collect(Collectors.toList());
    }

    @ApiOperation("支付方式")
    @GetMapping("/payType")
    public List<SysSet> payType(){
        List<SysSet> sysSets = sysSetService.getSysSets(SysSet.PAY_TYPE);
        return sysSets.stream().filter(sysSet -> !"4".equals(sysSet.getValue()) && !"5".equals(sysSet.getValue())).collect(Collectors.toList());
    }
    @ApiOperation("折扣类型")
    @GetMapping("/discountType")
    public List<SysSet> discountType() {
        List<SysSet> sysSets = sysSetService.getSysSets(SysSet.TYPE_MANAGE_FEE_DISCOUNT);

        return sysSets;
    }

    @ApiOperation("列表")
    @GetMapping("/page")
    public CustomPage<FlowingWaterVo> getPage(@ApiParam("当前页码") @RequestParam("pageNo") @NotNull(message = "请输入当前页码") Long pageNo,
                                              @ApiParam("每页条数") @RequestParam("pageSize") @NotNull(message = "请输入每页条数") Long pageSize,
                                              @ApiParam("小区id") @RequestParam(value = "villageId", required = false) Integer villageId,
                                              @ApiParam("开始时间") @RequestParam(value = "startTime", required = false) String startTime,
                                              @ApiParam("结束时间") @RequestParam(value = "endTime", required = false) String endTime,
                                              @ApiParam("楼栋") @RequestParam(value = "building", required = false) Integer building,
                                              @ApiParam("单元") @RequestParam(value = "unit", required = false) Integer unit,
                                              @ApiParam("房号") @RequestParam(value = "roomNumber", required = false) String roomNumber) {
        return flowingWaterService.page(pageNo, pageSize,villageId,building,unit,roomNumber,startTime,endTime);
    }

    @ApiOperation("物业列表")
    @GetMapping("/wuyePage")
    public CustomPage<FlowingWaterVo> getPage(@ApiParam("当前页码") @RequestParam("pageNo") @NotNull(message = "请输入当前页码") Long pageNo,
                                              @ApiParam("每页条数") @RequestParam("pageSize") @NotNull(message = "请输入每页条数") Long pageSize,
                                              @ApiParam("小区id") @RequestParam(value = "villageId", required = false) Integer villageId,
                                              @ApiParam("开始时间") @RequestParam(value = "startTime", required = false) String startTime,
                                              @ApiParam("结束时间") @RequestParam(value = "endTime", required = false) String endTime,
                                              @ApiParam("开票人") @RequestParam(value = "drawer", required = false) String drawer,
                                              @ApiParam("楼栋") @RequestParam(value = "building", required = false) String building,
                                              @ApiParam("单元") @RequestParam(value = "unit", required = false)String unit,
                                              @ApiParam("房号") @RequestParam(value = "roomNumber", required = false)String roomNumber) {
        return flowingWaterService.wuyePage(pageNo, pageSize, villageId, startTime, endTime,drawer,building,unit,roomNumber);
    }

    @ApiOperation("物业费票据")
    @GetMapping("/property/print/bill")
    public PayBillVo propertyPrintBill(@ApiParam("id") @RequestParam("id") @NotNull(message = "id为空") Integer id) {
        return flowingWaterService.propertyPrintBill(id);
    }

    @ApiOperation("物业列表上统计接口")
    @GetMapping("/statistics")
    public StatisticsVo statistics(
                                   @ApiParam("小区id") @RequestParam(value = "villageId", required = false) Integer villageId,
                                   @ApiParam("开始时间") @RequestParam(value = "startTime", required = false) String startTime,
                                   @ApiParam("结束时间") @RequestParam(value = "endTime", required = false) String endTime,
                                   @ApiParam("开票人") @RequestParam(value = "drawer", required = false) String drawer,
                                   @ApiParam("楼栋") @RequestParam(value = "building", required = false) String building,
                                   @ApiParam("单元") @RequestParam(value = "unit", required = false)String unit,
                                   @ApiParam("房号") @RequestParam(value = "roomNumber", required = false)String roomNumber) {
        return flowingWaterService.statistics( villageId, startTime, endTime,drawer,building,unit,roomNumber);
    }

    @ApiOperation("删除物业缴费记录")
    @PostMapping("/delete/wuye")
    public void deleteWuye(@RequestParam("id") @ApiParam("物业缴费编号") @NotNull(message = "物业缴费编号不能为空") Integer id){
        flowingWaterService.deleteWuye(id);
    }


    @ApiOperation("预存费用列表")
    @GetMapping("/getPrePage")
    public CustomPage<FlowingWaterVo> getPrePage(@ApiParam("当前页码") @RequestParam("pageNo") @NotNull(message = "请输入当前页码") Long pageNo,
                                              @ApiParam("每页条数") @RequestParam("pageSize") @NotNull(message = "请输入每页条数") Long pageSize,
                                              @ApiParam("小区id") @RequestParam(value = "villageId", required = false) Integer villageId,
                                              @ApiParam("开始时间") @RequestParam(value = "startTime", required = false) String startTime,
                                              @ApiParam("结束时间") @RequestParam(value = "endTime", required = false) String endTime,
                                              @ApiParam("票号") @RequestParam(value = "billNo", required = false) String billNo,
                                              @ApiParam("楼栋") @RequestParam(value = "building", required = false) String building,
                                              @ApiParam("单元") @RequestParam(value = "unit", required = false)String unit,
                                              @ApiParam("房号") @RequestParam(value = "roomNumber", required = false)String roomNumber) {
        return flowingWaterService.getPrePage(pageNo, pageSize, villageId, startTime, endTime, billNo,building,unit,roomNumber);
    }


    @ApiOperation("添加")
    @PostMapping("/add")
    public PayBillVo add(@ApiParam("小区id") @RequestParam(value = "villageId") @NotNull(message = "小区id不能为空") Integer villageId,
                    @ApiParam("楼栋id") @RequestParam(value = "buildingId",required = false)Integer buildingId,
                    @ApiParam("金额") @RequestParam(value = "money") @NotNull(message = "金额") BigDecimal money,
                    @ApiParam("收费名目") @RequestParam(value = "listOfCharges", required = false) String listOfCharges,
                    @ApiParam("收费类型") @RequestParam(value = "chargeType") @NotNull(message = "收费类型不能为空") Integer chargeType,
                    @ApiParam("付款方") @RequestParam(value = "payer",required = false)  String payer,
                    @ApiParam("收款方") @RequestParam(value = "payee", required = false) String payee,
                    @ApiParam("收款类型 1收入 2支出") @RequestParam(value = "type") @NotNull(message = "收款类型不能为空") Integer type,
                    @ApiParam("收款时间") @RequestParam(value = "paymentTime") @NotNull(message = "收款时间不能为空") LocalDate paymentTime,
                    @ApiIgnore @RequestParam("userId") @NotNull(message = "您尚未登录") Integer userId,
                    @ApiParam("打印票据") @RequestParam(value = "print",required = false, defaultValue = "false")Boolean print,
                    @ApiParam("备注") @RequestParam(value = "remark", required = false) String remark

    ) {
        return flowingWaterService.addOrUpdate(null, userId, villageId, buildingId, money, type, chargeType, listOfCharges, payer, payee, paymentTime, print, remark);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public void update(@RequestParam(value = "id") @NotNull(message = "id不能为空") Integer id,
                       @ApiParam("小区id") @RequestParam(value = "villageId") @NotNull(message = "小区id不能为空") Integer villageId,
                       @ApiParam("楼栋id") @RequestParam(value = "buildingId",required = false)Integer buildingId,
                       @ApiParam("金额") @RequestParam(value = "money") @NotNull(message = "金额") BigDecimal money,
                       @ApiParam("收费名目") @RequestParam(value = "listOfCharges", required = false) String listOfCharges,
                       @ApiParam("收费类型") @RequestParam(value = "chargeType") @NotNull(message = "收费类型不能为空") Integer chargeType,
                       @ApiParam("付款方") @RequestParam(value = "payer",required = false)  String payer,
                       @ApiParam("收款方") @RequestParam(value = "payee", required = false) String payee,
                       @ApiParam("收款类型 1收入 2支出") @RequestParam(value = "type") @NotNull(message = "收款类型不能为空") Integer type,
                       @ApiParam("收款时间") @RequestParam(value = "paymentTime") @NotNull(message = "收款时间不能为空") LocalDate paymentTime,
                       @ApiIgnore @RequestParam("userId") @NotNull(message = "您尚未登录") Integer userId,
                       @ApiParam("打印票据") @RequestParam(value = "print",required = false, defaultValue = "false")Boolean print,
                       @ApiParam("备注") @RequestParam(value = "remark", required = false) String remark
    ) {
        flowingWaterService.addOrUpdate(id, userId, villageId, buildingId, money, type, chargeType, listOfCharges, payer, payee, paymentTime, print, remark);
    }





    @ApiOperation("新增预收")
    @PostMapping("/addPro")
    public PayBillVo addPro(@ApiParam("小区id") @RequestParam(value = "villageId") @NotNull(message = "小区id不能为空") Integer villageId,
                            @ApiParam("实收金额") @RequestParam(value = "net") @NotNull(message = "实收金额") BigDecimal net,
                            @ApiParam("楼栋id") @RequestParam(value = "buildingId")@NotNull(message = "楼栋id不能为空")Integer buildingId,
                            @ApiParam("收款方式") @RequestParam(value = "payType",required = false) String payType,
                            @ApiParam("是否打印票据 打印传1") @RequestParam(value = "printing",required = false) String printing,
                            @ApiParam("备注") @RequestParam(value = "remark", required = false) String remark,
                            @ApiParam("缴费类型") @RequestParam(value = "walletType", required = false) WalletType walletType,
                            @ApiIgnore @RequestParam("userId") @NotNull(message = "您尚未登录") Integer userId) {
        return flowingWaterService.addPro(userId,villageId,buildingId,net,printing,payType, remark, walletType);
    }

    @ApiOperation("删除预收")
    @PostMapping("/delete/pro")
    public void deletePro(@RequestParam("id") @ApiParam("预收费编号") @NotNull(message = "预收费编号不能为空") Integer id){
        flowingWaterService.deletePro(id);
    }

    @ApiOperation("预存费票据")
    @GetMapping("/advance/print/bill")
    public PayBillVo advancePrintBill(@ApiParam("id") @RequestParam("id") @NotNull(message = "id为空") Integer id) {
        return flowingWaterService.advancePrintBill(id);
    }

    /*@ApiOperation("删除")
    @PostMapping("/delete")
    public void delete(@ApiParam("id") @RequestParam("id") @NotNull(message = "id为空") Integer id) {
        flowingWaterService.delete(id);
    }*/

    @ApiOperation("获取收入、支出、总余额")
    @GetMapping("/getMoney")
    public FlowingMoneyVo getMoney(@ApiParam("小区id") @RequestParam(value = "villageId", required = false) Integer villageId,
                                   @ApiParam("开始时间") @RequestParam(value = "startTime", required = false) String startTime,
                                   @ApiParam("结束时间") @RequestParam(value = "endTime", required = false) String endTime,
                                   @ApiParam("楼栋") @RequestParam(value = "building", required = false) Integer building,
                                   @ApiParam("单元") @RequestParam(value = "unit", required = false) Integer unit,
                                   @ApiParam("房号") @RequestParam(value = "roomNumber", required = false) String roomNumber) {
        return flowingWaterService.getMoney(villageId, startTime, endTime, building, unit, roomNumber);
    }



    @ApiOperation("打印票据")
    @PostMapping("/print/bill")
    public PayBillVo printBill(@ApiParam("id") @RequestParam("id") @NotNull(message = "id为空") Integer id) {
        return flowingWaterService.printBill(id);
    }

    @ResponseBody
    @GetMapping("/downOwner")
    @ApiOperation(value = "财务报表下载", responseContainer = "List", notes = "下载")
    public void downOwner(HttpServletRequest request, HttpServletResponse response,
                          @ApiParam("开始时间") @RequestParam(value = "startTime") String startTime,
                          @ApiParam("结束时间") @RequestParam(value = "endTime") String endTime,
                          @ApiParam("小区id") @RequestParam(value = "villageId", required = false) Integer villageId) {
        flowingWaterService.downOwner(request,response,villageId,startTime,endTime);
    }

    @ApiOperation("新增预收费用根据楼栋id回显数据")
    @ResponseBody
    @GetMapping("/findProBuildingVo")
    public ProBuildingVo findProBuildingVo( @ApiParam("楼栋id") @RequestParam(value = "buildingId") Integer buildingId, @ApiParam("缴费类型") WalletType walletType){
        return flowingWaterService.findProBuildingVo(buildingId, walletType);
    }


    @ApiOperation("月每日流水统计")
    @ResponseBody
    @GetMapping("/selectRoutine")
    public TotalVo selectRoutine(@ApiParam("时间 格式 yyyy-mm") @RequestParam(value = "time") String time){
        return flowingWaterService.selectRoutine(time);
    }

    @ApiOperation("月每日流水统计导出")
    @ResponseBody
    @GetMapping("/exportExcel")
    public void exportExcel(@ApiParam("时间 格式 yyyy-mm") @RequestParam(value = "time") String time,HttpServletRequest request, HttpServletResponse response) {
        flowingWaterService.exportExcel(time,request,response);
    }

    @ApiOperation("楼栋流水统计列表")
    @ResponseBody
    @GetMapping("/selectRoutineByBuildingId")
    public TotalVo selectRoutine(@ApiParam("时间 格式 yyyy-mm-dd") @RequestParam(value = "startTime")@NotBlank(message = "开始时间不能为空") String startTime,
                                 @ApiParam("时间 格式 yyyy-mm-dd") @RequestParam(value = "endTime")@NotBlank(message = "结束时间不能为空") String endTime,
                                 @ApiParam("小区id") @RequestParam(value = "villageId", required = false) Integer villageId
                                 ){

        return flowingWaterService.selectRoutine(startTime,endTime,villageId);
    }

    @ApiOperation("每栋流水统计导出")
    @ResponseBody
    @GetMapping("/exportExcelBuilding")
    public void exportExcelBuilding(@ApiParam("时间 格式 yyyy-mm-dd") @RequestParam(value = "startTime")@NotBlank(message = "开始时间不能为空") String startTime,
                                    @ApiParam("时间 格式 yyyy-mm-dd") @RequestParam(value = "endTime")@NotBlank(message = "结束时间不能为空") String endTime,
                                    @ApiParam("小区id") @RequestParam(value = "villageId", required = false) Integer villageId,HttpServletRequest request, HttpServletResponse response) {
        flowingWaterService.exportExcel(startTime,endTime,villageId,request,response);
    }

    @ApiOperation("月统计报表")
    @ResponseBody
    @GetMapping("/select/month/statistic")
    public MonthStatisticVo selectMonthStatistic(@ApiParam("小区id") @RequestParam("villageId") @NotNull(message = "请先选择小区") Integer villageId,
                                                 @ApiParam("月份:yyyy-MM") @RequestParam("month") @NotBlank(message = "请先选择月份") String month){
        return flowingWaterService.monthStatisticVo(villageId, month);
    }

    @ApiOperation("月统计报表导出")
    @ResponseBody
    @GetMapping("/export/month/statistic")
    public void exportMonthStatistic(@ApiParam("小区id") @RequestParam("villageId") @NotNull(message = "请先选择小区") Integer villageId,
                                     @ApiParam("月份:yyyy-MM") @RequestParam("month") @NotBlank(message = "请先选择月份") String month,
                                     HttpServletRequest request, HttpServletResponse response){
        flowingWaterService.exportMonthStatistic(villageId, month, request, response);
    }
}
